package com.example.a15787.weather_zyy_1235;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class ForecastAdapter extends RecyclerView.Adapter<ForecastAdapter.ViewHolder> {
    private List<WeatherResponse.Forecast> forecastList;

    public ForecastAdapter(List<WeatherResponse.Forecast> forecastList) {
        this.forecastList = forecastList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_item_forecast, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        WeatherResponse.Forecast forecast = forecastList.get(position);
        holder.tvDate.setText(forecast.getDate());
        holder.tvWeatherType.setText(forecast.getWeather());
        holder.tvTemperatureRange.setText(forecast.getTemperature().getMin() + " ~ " +
                forecast.getTemperature().getMax() + "℃");

        int iconResId = getIconResource(forecast.getIcon());
        holder.ivIcon.setImageResource(iconResId);
    }

    @Override


    public int getItemCount() {
        return forecastList.size();
    }

    public static int getIconResource(String icon) {
        switch (icon) {
            case "sunny":
                return R.drawable.ic_sunny;
            case "cloudy":
                return R.drawable.ic_cloudy;
            case "light-rain":
                return R.drawable.light_rain;
            case "moderate-rain":
                return R.drawable.moderate_rain;
            case "thunder-shower":
                return R.drawable.thunder_shower;
            case "cloudy-to-sunny":
                return R.drawable.cloudy_to_sunny;
            case "rain-to-cloudy":
                return R.drawable.rain_to_cloudy;
            case "thunder-to-cloudy":
                return R.drawable.thunder_to_cloudy;
            case "sunny-to-cloudy":
                return R.drawable.sunny_to_cloudy;
            default:
                return R.drawable.ic_default; // 默认图标
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvDate;
        TextView tvWeatherType;
        TextView tvTemperatureRange;
        ImageView ivIcon;

        ViewHolder(View itemView) {
            super(itemView);
            tvDate = (TextView) itemView.findViewById(R.id.tvDate);
            tvWeatherType = (TextView) itemView.findViewById(R.id.tvWeatherType);
            tvTemperatureRange = (TextView) itemView.findViewById(R.id.tvTemperatureRange);
            ivIcon = (ImageView) itemView.findViewById(R.id.ivIcon);
        }
    }
}